package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.server.model.FeeScale;
import com.ybkj.daijia.server.service.FeeScaleService;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author tanglei
 */
@Controller
@RequestMapping(value = "feeScale")
public class FeeScaleController {

    @Autowired
    private FeeScaleService feeScaleService;

    @Autowired
    private SettingUtils settingUtils;

    /**
     * 查询所有收费标准.
     *
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView list() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isPermitted("activity")) {
            ModelAndView mv = new ModelAndView();
            List<FeeScale> data = feeScaleService.findAll();
            for (int i = 0; i < data.size(); i++) {
                String FareValidOns = data.get(i).getFareValidOn().substring(0, 10);
                data.get(i).setFareValidOn(FareValidOns);
                String FareValidOffs = data.get(i).getFareValidOff().substring(0, 10);
                data.get(i).setFareValidOff(FareValidOffs);
                mv.addObject("FareValidOns", FareValidOns);
                mv.addObject("FareValidOffs", FareValidOffs);
            }
            mv.addObject("FeeScale", data);
            mv.addObject("totalRows", data.size());
            mv.addObject("isUpPlatformInfo", settingUtils.get().isUpPlatformInfo());

            mv.setViewName("feeScale/list");
            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    /**
     * 根据id查询单个收费标准
     *
     * @param id
     * @param req
     * @return
     */
    @RequestMapping(value = "findOne/{id}", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> findOne(@PathVariable("id") Long id, HttpServletRequest req) {
        FeeScale feeScale = feeScaleService.findById(id);
        Map<String, Object> map = new HashMap<String, Object>();
        String FareValidOns = feeScale.getFareValidOn().substring(0, 10);
        String FareValidOffs = feeScale.getFareValidOff().substring(0, 10);
        map.put("object", feeScale);
        map.put("FareValidOns", FareValidOns);
        map.put("FareValidOffs", FareValidOffs);
        return map;
    }

    /**
     * 根据id删除收费标准.
     *
     * @param id
     * @param req
     * @return
     */
    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public @ResponseBody
    Result<Integer> delete(@PathVariable("id") Long id, HttpServletRequest req) {
        Result<Integer> result = new Result<Integer>();
        if (null != id) {
            int code = feeScaleService.deleteById(id);
            if (code == 1) {
                result.setSuccess(true);
                result.setMessage("删除成功");
            } else {
                result.setSuccess(false);
                result.setMessage("删除失败");
            }
        } else {
            result.setSuccess(false);
            result.setMessage("参数错误");
        }
        return result;
    }

    /**
     * 根据id修改收费标准
     *
     * @param id
     * @param req
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateOne(
        @RequestParam(name = "minPrice", required = false) BigDecimal minPrice,
        @RequestParam(name = "vehicleType", required = false) String vehicleType,
        @RequestParam(name = "valid", required = false) Integer valid,
        @RequestParam(name = "feeId", required = false) Long feeId,
        @RequestParam(name = "fareType", required = false) String fareType,
        @RequestParam(name = "fareTypeNote", required = false) String fareTypeNote,
        @RequestParam(name = "fareValidOn", required = false) String fareValidOn,
        @RequestParam(name = "fareValidOff", required = false) String fareValidOff,
        @RequestParam(name = "startFare", required = false) BigDecimal startFare,
        @RequestParam(name = "startMile", required = false) Double startMile,
        @RequestParam(name = "startMinute", required = false) Double startMinute,
        @RequestParam(name = "unitPricePerMile", required = false) BigDecimal unitPricePerMile,
        @RequestParam(name = "unitPricePerMinute", required = false) BigDecimal unitPricePerMinute,
        @RequestParam(name = "morningPeakTimeOn", required = false) String morningPeakTimeOn,
        @RequestParam(name = "morningPeakTimeOff", required = false) String morningPeakTimeOff,
        @RequestParam(name = "eveningPeakTimeOn", required = false) String eveningPeakTimeOn,
        @RequestParam(name = "eveningPeakTimeOff", required = false) String eveningPeakTimeOff,
        @RequestParam(name = "otherPeakTimeOn", required = false) String otherPeakTimeOn,
        @RequestParam(name = "otherPeakTimeOff", required = false) String otherPeakTimeOff,
        @RequestParam(name = "nightTimeStart", required = false) String nightTimeStart,
        @RequestParam(name = "nightTimeEnd", required = false) String nightTimeEnd,
        @RequestParam(name = "singleUnitPrice", required = false) BigDecimal singleUnitPrice,
        @RequestParam(name = "singlePriceStartMile", required = false) Double singlePriceStartMile,
        @RequestParam(name = "peakUnitPrice", required = false) BigDecimal peakUnitPrice,
        @RequestParam(name = "peakPriceStartMile", required = false) Double peakPriceStartMile,
        @RequestParam(name = "lowSpeedPricePerMinute", required = false) BigDecimal lowSpeedPricePerMinute,
        @RequestParam(name = "nightPricePerMile", required = false) BigDecimal nightPricePerMile,
        @RequestParam(name = "nightPricePerMinute", required = false) BigDecimal nightPricePerMinute,
        @RequestParam(name = "callCharge", required = false) BigDecimal callCharge,
        @RequestParam(name = "extraCharge", required = false) BigDecimal extraCharge,
        @RequestParam(name = "otherPrice", required = false) BigDecimal otherPrice,
        HttpServletRequest req) throws Exception {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            FeeScale fs = feeScaleService.findByFareTypeAndId(fareType, feeId);
            if (fs != null) {
                map.put("fail", "运价类型已存在");
            } else {
                FeeScale feeScale = new FeeScale();
                feeScale.setMinPrice(minPrice);
                feeScale.setVehicleType(vehicleType);
                feeScale.setValid(valid);
                feeScale.setId(feeId);
                feeScale.setStartMinute(startMinute);
                feeScale.setSingleUnitPrice(singleUnitPrice);
                feeScale.setSinglePriceStartMile(singlePriceStartMile);
                feeScale.setPeakUnitPrice(peakUnitPrice);
                feeScale.setPeakPriceStartMile(peakPriceStartMile);
                feeScale.setLowSpeedPricePerMinute(lowSpeedPricePerMinute);
                feeScale.setNightPricePerMile(nightPricePerMile);
                feeScale.setNightPricePerMinute(nightPricePerMinute);
                feeScale.setFareType(fareType);
                feeScale.setFareTypeNote(fareTypeNote);
                feeScale.setFareValidOn(fareValidOn);
                feeScale.setFareValidOff(fareValidOff);
                feeScale.setStartFare(startFare);
                feeScale.setStartMile(startMile);
                feeScale.setUnitPricePerMile(unitPricePerMile);
                feeScale.setUnitPricePerMinute(unitPricePerMinute);
                feeScale.setMorningPeakTimeOn(morningPeakTimeOn);
                feeScale.setMorningPeakTimeOff(morningPeakTimeOff);
                feeScale.setEveningPeakTimeOn(eveningPeakTimeOn);
                feeScale.setEveningPeakTimeOff(eveningPeakTimeOff);
                feeScale.setOtherPeakTimeOn(otherPeakTimeOn);
                feeScale.setOtherPeakTimeOff(otherPeakTimeOff);
                feeScale.setNightTimeStart(nightTimeStart);
                feeScale.setNightTimeEnd(nightTimeEnd);
                feeScale.setCallCharge(callCharge);
                feeScale.setExtraCharge(extraCharge);
                feeScale.setOtherPrice(otherPrice);
                int up = feeScaleService.updateById(feeScale);
                if (up > 0) {
                    map.put("success", "修改成功");
                } else {
                    map.put("fail", "修改失败");
                }
            }
            return map;
        } catch (Exception e) {

            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加收费标准.
     *
     * @param req
     * @return
     */
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public @ResponseBody
    Result<Integer> save(@RequestParam(name = "minPrice", required = false) BigDecimal minPrice,
        @RequestParam(name = "vehicleType", required = false) String vehicleType,
        @RequestParam(name = "valid", required = false) Integer valid,
        @RequestParam(name = "fareType", required = false) String fareType,
        @RequestParam(name = "fareTypeNote", required = false) String fareTypeNote,
        @RequestParam(name = "fareValidOn", required = false) String fareValidOn,
        @RequestParam(name = "fareValidOff", required = false) String fareValidOff,
        @RequestParam(name = "startFare", required = false) BigDecimal startFare,
        @RequestParam(name = "startMile", required = false) Double startMile,
        @RequestParam(name = "startMinute", required = false) Double startMinute,
        @RequestParam(name = "unitPricePerMile", required = false) BigDecimal unitPricePerMile,
        @RequestParam(name = "unitPricePerMinute", required = false) BigDecimal unitPricePerMinute,
        @RequestParam(name = "morningPeakTimeOn", required = false) String morningPeakTimeOn,
        @RequestParam(name = "morningPeakTimeOff", required = false) String morningPeakTimeOff,
        @RequestParam(name = "eveningPeakTimeOn", required = false) String eveningPeakTimeOn,
        @RequestParam(name = "eveningPeakTimeOff", required = false) String eveningPeakTimeOff,
        @RequestParam(name = "otherPeakTimeOn", required = false) String otherPeakTimeOn,
        @RequestParam(name = "otherPeakTimeOff", required = false) String otherPeakTimeOff,
        @RequestParam(name = "nightTimeStart", required = false) String nightTimeStart,
        @RequestParam(name = "nightTimeEnd", required = false) String nightTimeEnd,
        @RequestParam(name = "singleUnitPrice", required = false) BigDecimal singleUnitPrice,
        @RequestParam(name = "singlePriceStartMile", required = false) Double singlePriceStartMile,
        @RequestParam(name = "peakUnitPrice", required = false) BigDecimal peakUnitPrice,
        @RequestParam(name = "peakPriceStartMile", required = false) Double peakPriceStartMile,
        @RequestParam(name = "lowSpeedPricePerMinute", required = false) BigDecimal lowSpeedPricePerMinute,
        @RequestParam(name = "nightPricePerMile", required = false) BigDecimal nightPricePerMile,
        @RequestParam(name = "nightPricePerMinute", required = false) BigDecimal nightPricePerMinute,
        @RequestParam(name = "callCharge", required = false) BigDecimal callCharge,
        @RequestParam(name = "extraCharge", required = false) BigDecimal extraCharge,
        @RequestParam(name = "otherPrice", required = false) BigDecimal otherPrice,
        HttpServletRequest req) throws Exception {

        Result<Integer> result = new Result<Integer>();
        FeeScale fs = feeScaleService.findByFareType(fareType);
        if (fs != null) {
            result.setSuccess(false);
            result.setMessage("运价类型已存在");
        } else {
            FeeScale feeScale = new FeeScale();
            feeScale.setMinPrice(minPrice);
            feeScale.setVehicleType(vehicleType);
            feeScale.setValid(valid);

            feeScale.setStartMinute(startMinute);
            feeScale.setSingleUnitPrice(singleUnitPrice);
            feeScale.setSinglePriceStartMile(singlePriceStartMile);
            feeScale.setPeakUnitPrice(peakUnitPrice);
            feeScale.setPeakPriceStartMile(peakPriceStartMile);
            feeScale.setLowSpeedPricePerMinute(lowSpeedPricePerMinute);
            feeScale.setNightPricePerMile(nightPricePerMile);
            feeScale.setNightPricePerMinute(nightPricePerMinute);
            feeScale.setFareType(fareType);
            feeScale.setFareTypeNote(fareTypeNote);
            feeScale.setFareValidOn(fareValidOn);
            feeScale.setFareValidOff(fareValidOff);
            feeScale.setStartFare(startFare);
            feeScale.setStartMile(startMile);
            feeScale.setUnitPricePerMile(unitPricePerMile);
            feeScale.setUnitPricePerMinute(unitPricePerMinute);
            feeScale.setMorningPeakTimeOn(morningPeakTimeOn);
            feeScale.setMorningPeakTimeOff(morningPeakTimeOff);
            feeScale.setEveningPeakTimeOn(eveningPeakTimeOn);
            feeScale.setEveningPeakTimeOff(eveningPeakTimeOff);
            feeScale.setOtherPeakTimeOn(otherPeakTimeOn);
            feeScale.setOtherPeakTimeOff(otherPeakTimeOff);
            feeScale.setNightTimeStart(nightTimeStart);
            feeScale.setNightTimeEnd(nightTimeEnd);
            feeScale.setCallCharge(callCharge);
            feeScale.setExtraCharge(extraCharge);
            feeScale.setOtherPrice(otherPrice);
            int code = feeScaleService.save(feeScale);
            if (code > 0) {
                result.setSuccess(true);
                result.setMessage("添加成功");
            } else {
                result.setSuccess(false);
                result.setMessage("添加失败");
            }
        }

        return result;
    }

    /**
     * 上报到监管平台
     *
     * @param id
     * @param req
     * @return
     */
    @RequestMapping(value = "report/{id}", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> Report(@PathVariable("id") Long id, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        FeeScale feeScale = feeScaleService.findById(id);
        if (feeScaleService.Report(feeScale)) {
            map.put("message", "上报成功");
        } else {
            map.put("message", "上报失败");
        }
        return map;
    }

}
